import StatusNav from '@/components/school/statusNav';
import PageBody from '@/components/school/pageBody';
import {mapGetters} from "vuex";
import * as ProductApi from '@/api/school/product.js';
import diyJson from '@/config/diy.js';
import {BASE_CONFIG} from "@/config/app"
import {isBlank} from "@/utils/util";
export const mixins =
    {
      computed: mapGetters(['uid', 'userInfo', 'shareInfo', 'schoolId']),
      components: {
        StatusNav,
        PageBody,
      },
      data() {
        return {
          stickyTop:0,
          themConfig: diyJson.commonStyle,
          loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
          },
          product: {
            list: [],
            activeKey: 0,
            typeList: [],
          },
          statusActiveKey:0,
          statusList:[
            {
              name:'全部',
              value:''
            },
            {
              name:'已售出',
              value:'SOLD'
            },
            {
              name:'转让中',
              value:'NORMAL'
            }
          ],
          isReachBottom: false,
          loading: {
            status: 'nomore',
            loadingText: '加载中',
            loadmoreText: '点击加载更多',
          },
          dataPage: {
            nextId: undefined,
            hasNext: false,
            ext: undefined
          },
          searchParams: {
            typeNos: '',
            status:''
          },
        };
      },

      onLoad: function (options) {

      },

      onShow() {
        this.getProductData();
        this.getTypeList();
      },

      methods: {
        getTypeList() {
          ProductApi.typeList().then(res => {
            let all = {name: "全部", value: ''}
            let arr = [];
            arr.push(all);
            res.data.forEach(e => {
              arr.push(e)
            })
            this.product.typeList = arr;
          })
        }
        ,

        tabOnChange(e) {
          let index = e.index
          let type = this.product.typeList[index];
          let value = type.value;
          if (index === 0) {
            value = '';
          }
          this.searchParams.typeNo = value;
          this.getProductData()
        },

        tabStatusOnChange(e){
          let index = e.index
          let type = this.statusList[index];
          let value = type.value;
          if (index === 0) {
            value = '';
          }
          this.searchParams.status = value;
          this.getProductData()
        },

        toDetail(item) {
          uni.navigateTo({
            url: "/pages/school/product-idle/detail/index?productNo=" + item.productNo
          })
        }
        ,
        saleProduct(item,index) {
          ProductApi.updateStatus({productNo: item.productNo}).then(res => {
            this.product.list[index].status = "SOLD";
          })
        },
        polishProduct(item,index) {
          ProductApi.updatePolish({productNo: item.productNo}).then(res => {
            uni.showToast({
              title: '已擦亮',
              icon: 'none'
            })
          })
        },
        shareCopy(item){
          uni.setClipboardData({
            data: BASE_CONFIG.H5WebHost+'/pages/school/product-idle/detail/index?productNo='+item.productNo,
            success: function () {
              uni.showToast({
                title: '分享链接已复制',
                icon: 'success',
                duration: 2000
              });
            }
          });
        },
        updateProduct(e) {
          uni.navigateTo({
            url:'/pages/school/product-idle/add/index?productNo='+e.productNo
          })
        },
        getProductData() {
          ProductApi.myProductList(this.searchParams).then(
              res => {
                this.product.list = res.data.records
                this.dataPage.hasNext = res.data.hasNext;
                if (res.data.nextId) {
                  this.dataPage.nextId = res.data.nextId;
                }
              })
        },
        scrolltolower() {
          this.isReachBottom = true;
          this.loading.status = 'nomore';
          if (this.dataPage.hasNext) {
            this.loading.status = 'loadmore';
          }
        },
        loadmore(){
          if (this.dataPage.hasNext) {
            this.loading.status = 'loading';
            ProductApi.myProductList({...this.searchParams,nextId: this.dataPage.nextId}).then(
                res => {
                  res.data.records.forEach(item => {
                    this.product.list.push(item)
                  })

                  this.dataPage.hasNext = res.data.hasNext;
                  if (res.data.nextId) {
                    this.dataPage.nextId = res.data.nextId;
                  }
                  if (res.data.hasNext){
                    this.loading.status = 'loadmore';
                  }else {
                    this.loading.status = 'nomore';
                  }
                })
          }
        },

        clickBtn() {
          if ((isBlank(this.userInfo.phone) || isBlank(this.schoolId))){
            uni.showToast({
              title: '需要先注册信息......',
              duration: 1000,
              icon: 'none'
            });
            setTimeout(() => {
              uni.navigateTo({
                url: '/pages/users/user_info/index',
              })
            }, 500)
          }else {
            uni.navigateTo({url: '/pages/users/my-group/add/index'});
          }
        }
        ,
      }
      ,
      onReachBottom() {
        this.scrolltolower();
      }
    }
